package com.java.service;

import com.java.entity.vo.WeChatTokenResponse;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

@Service
public class WeChatTokenService {

    private static final String WX_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token";

    // 核心获取 access_token 方法
    public  WeChatTokenResponse getAccessToken(String appid, String secret) {
        RestTemplate restTemplate = new RestTemplate();

        // 构建 url，拼接 query 参数
        String url = UriComponentsBuilder.fromHttpUrl(WX_TOKEN_URL)
                .queryParam("grant_type", "client_credential")
                .queryParam("appid", appid)
                .queryParam("secret", secret)
                .toUriString();

        WeChatTokenResponse response = restTemplate.getForObject(url, WeChatTokenResponse.class);
        return response;
    }
}